package com.hang.gymmanager.controller;

import cn.hutool.http.ContentType;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hang.gymmanager.common.BasePageReq;
import com.hang.gymmanager.common.Response;
import com.hang.gymmanager.config.AlipayConfig;
import com.hang.gymmanager.entity.AlipayOrderEntity;
import com.hang.gymmanager.entity.WalletEntity;
import com.hang.gymmanager.exception.BusinessException;
import com.hang.gymmanager.req.AddOrderReq;
import com.hang.gymmanager.req.AliPayReq;
import com.hang.gymmanager.req.UpdatePasswordReq;
import com.hang.gymmanager.res.CenterCoursePageRes;
import com.hang.gymmanager.res.CenterOrderPageRes;
import com.hang.gymmanager.res.CenterPlacePageRes;
import com.hang.gymmanager.res.CenterTicketPageRes;
import com.hang.gymmanager.service.*;
import com.hang.gymmanager.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 用户中心控制器
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/4/3 19:02
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/userCenter")
@Slf4j
@CrossOrigin
public class UserCenterController {

    private final UserCenterService userCenterService;
    private final AlipayConfig alipayConfig;
    private final AlipayService alipayService;

    private final AlipayOrderService alipayOrderService;

    private final WalletService walletService;
    private final UserService userService;

    /**
     * 订单查询
     * @return
     */
    @PostMapping("/order")
    public Response<Page<CenterOrderPageRes>> order(@RequestBody BasePageReq req){
        Page<CenterOrderPageRes> res = userCenterService.order(req);
        return Response.success(res);
    }

    /**
     * 课程查询
     * @return
     */
    @PostMapping("/course")
    public Response<Page<CenterCoursePageRes>> course(@RequestBody BasePageReq req){
        Page<CenterCoursePageRes> res = userCenterService.course(req);
        return Response.success(res);
    }

    /**
     * 门票查询
     * @return
     */
    @PostMapping("/ticket")
    public Response<Page<CenterTicketPageRes>> ticket(@RequestBody BasePageReq req){
        Page<CenterTicketPageRes> res = userCenterService.ticket(req);
        return Response.success(res);
    }

    /**
     * 场地预约查询
     * @return
     */
    @PostMapping("/place")
    public Response<Page<CenterPlacePageRes>> place(@RequestBody BasePageReq req){
        Page<CenterPlacePageRes> res = userCenterService.place(req);
        return Response.success(res);
    }

    /**
     * 重置密码
     * @return
     */
    @GetMapping("/reset")
    public Response reset(){
        userCenterService.reset();
        return Response.success();
    }

    /**
     * 充值订单生成
     * @param aliPayReq
     * @param response
     * @throws IOException
     */
    @GetMapping("/recharge")
    public void recharge(AliPayReq aliPayReq, HttpServletResponse response) throws IOException {
        response.setContentType(ContentType.TEXT_HTML.getValue() + ";charset=" + alipayConfig.getCharset());
        Map<String, String> map = alipayService.initiatePcPayment(aliPayReq);
        response.getWriter().write(map.get("formHtml"));
        response.getWriter().flush();
        response.getWriter().close();
    }

    /**
     * 创建订单
     * @return
     */
    @PostMapping("/createOrder")
    public Response<?> createOrder(@RequestBody AddOrderReq req) {
        AlipayOrderEntity order = alipayOrderService.createOrder(req);
        return Response.success(order);
    }

    /**
     * 查询用户余额
     * @return
     */
    @GetMapping("/getBalance")
    public Response<BigDecimal> createOrder() {
        Integer userId = SecurityUtils.getUserId();
        WalletEntity userWallet = walletService.getById(userId);
        return Response.success(userWallet.getBalance());
    }

    /**
     * 查询用户余额
     * @return
     */
    @PostMapping("/updatePassword")
    public Response updatePassword(@RequestBody UpdatePasswordReq req) {
        if(req.getOldPwd()==null||req.getOldPwd().equals("")){
            throw new BusinessException("旧密码不能为空");
        }
        if(req.getNewPwd()==null||req.getNewPwd().equals("")){
            throw new BusinessException("新密码不能为空");
        }
        if(req.getCheckNewPwd()==null||req.getCheckNewPwd().equals("")){
            throw new BusinessException("确认密码不能为空");
        }
        userService.updatePassword(req);
        return Response.success();
    }

}
